﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using System.IO;
using MvcLibrary.Repository;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminDestinationAreaController : Controller
    {
        //
        // GET: /AdminCP/AdminTravelProduction/

        public ActionResult Index(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityDestinationAreas
                            where p.IdDestinationInfo == id
                            select new AdminDestinationAreaModel
                            {
                                Id = p.Id,
                                IdDestinationInfo = p.IdDestinationInfo,
                                Name = p.Name,
                                Description = p.Description,
                                Image = p.Image,
                                Position = p.Position
                            };
                          
                return View(query.ToList());
            }
        }


        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityDestinationAreas
                            where p.Id == id
                            select new AdminDestinationAreaModel
                            {
                                Id = p.Id,
                                IdDestinationInfo = p.IdDestinationInfo,
                                Name = p.Name,
                                Description = p.Description,
                                Image = p.Image,
                                Position = p.Position,
                                Meta_description = p.Meta_description,
                                Meta_keyword = p.Meta_keyword,
                                Titletag = p.Titletag
                            };
                return View(query.FirstOrDefault());
            }
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminDestinationAreaModel model,HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityDestinationArea Area = db.EntityDestinationAreas.FirstOrDefault(p => p.Id == model.Id);
                if (Area != null)
                {
                    Area.IdDestinationInfo = 1;
                    Area.Name = model.Name;
                    Area.Description = model.Description;
                    Area.Position = model.Position;
                    Area.Meta_keyword = model.Meta_keyword;
                    Area.Meta_description = model.Meta_description;
                    Area.Titletag = model.Titletag;
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (Area.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Area.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                        Image.SaveAs(path);

                        Area.Image = encodestring;
                    }
                    db.SubmitChanges();

                    return RedirectToAction("Index", new { id = 1 });
                }
                else
                    return View("Error");
                
            }
        }


        public ActionResult Insert()
        {
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminDestinationAreaModel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityDestinationArea Area = new EntityDestinationArea();
                Area.IdDestinationInfo = 1;
                Area.Name = model.Name;
                Area.Description = model.Description;
                Area.Position = model.Position;
                Area.Meta_keyword = model.Meta_keyword;
                Area.Meta_description = model.Meta_description;
                Area.Titletag = model.Titletag;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                    }
                    //delete file cu
                    if (Area.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Area.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                    Image.SaveAs(path);

                    Area.Image = encodestring;
                }
                db.EntityDestinationAreas.InsertOnSubmit(Area);
                db.SubmitChanges();

                return RedirectToAction("Index", new { id = 1 });
            }
        }
        public string Delete(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var delquery = db.EntityDestinationCountries.Where(p => p.IdDestinationArea == id).ToList();
                db.EntityDestinationCountries.DeleteAllOnSubmit(delquery);
                EntityDestinationArea Area = db.EntityDestinationAreas.FirstOrDefault(p => p.Id == id);
                if (Area != null)
                {
                    if (Area.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Area.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    db.EntityDestinationAreas.DeleteOnSubmit(Area);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";
            }
        }
    }
}
